package com.ylbh.modules.hotwords.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ylbh.common.pojo.hotwords.dto.HotWordsDTO;
import com.ylbh.common.utils.IdWorker;
import com.ylbh.common.utils.PageUtils;
import com.ylbh.common.utils.ShiroUtils;
import com.ylbh.modules.hotwords.entity.ShoppingHotWords;
import com.ylbh.modules.hotwords.mapper.ShoppingHotWordMapper;
import com.ylbh.modules.hotwords.service.ShoppingHotWordsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @author: czx
 * @date: 2019/08/03 18:02
 */
@Service
public class ShoppingHotWordsServiceImpl extends ServiceImpl<ShoppingHotWordMapper, ShoppingHotWords> implements ShoppingHotWordsService {

    @Autowired
    private ShoppingHotWordMapper shoppingHotWordMapper;

    /**
     * 获取搜索热词列表
     * @return
     */
    @Override
    public PageUtils queryHotWordsList(HotWordsDTO hotWordsDTO) {
        Page<Integer> page = new Page<>(hotWordsDTO.getPage(), hotWordsDTO.getLimit());
        List<ShoppingHotWords> list = shoppingHotWordMapper.selectHotWordsList(hotWordsDTO, page);
        return new PageUtils(new Page<ShoppingHotWords>().setRecords(list).setTotal(page.getTotal()));
    }

    /**
     * 获取搜索热词详情
     * @param id
     */
    @Override
    public ShoppingHotWords getHotWordsList(String id) {
        return shoppingHotWordMapper.selectById(id);
    }

    /**
     * 修改热词搜索详情
     * @param shoppingHotWords
     */
    @Override
    public void modifyHotWords(ShoppingHotWords shoppingHotWords) {
        shoppingHotWords.setUpdateUserId(ShiroUtils.getUserId().intValue());
        shoppingHotWordMapper.updateById(shoppingHotWords);
    }

    /**
     * 新增热词搜索
     * @param shoppingHotWords
     */
    @Override
    public void addHotWords(ShoppingHotWords shoppingHotWords) {
        shoppingHotWords.setId(new IdWorker().nextId());
        shoppingHotWords.setHotPrice(new BigDecimal("0.00"));
        shoppingHotWords.setCreateTime(new Date());
        shoppingHotWords.setHotUserId(1);
        shoppingHotWords.setHotClick(0);
        shoppingHotWords.setUpdateUserId(ShiroUtils.getUserId().intValue());
        shoppingHotWordMapper.insertHotWords(shoppingHotWords);
    }

    /**
     * 热词上下架操作
     * @param id
     * @param hotSale
     */
    @Override
    public void modifyUpperAndLowerRacksById(String id, Integer hotSale) {
        ShoppingHotWords words = new ShoppingHotWords();
        words.setId(id);
        words.setHotSale(hotSale);
        words.setUpdateUserId(ShiroUtils.getUserId().intValue());
        shoppingHotWordMapper.updateById(words);
    }

    /**
     * 删除热词
     * @param ids
     */
    @Override
    public void removeHotWordsById(List<String> ids) {
        shoppingHotWordMapper.deleteHotWordsById(new Date(), ShiroUtils.getUserId(), ids);
    }

    /**
     * 热词类型数量
     * @return
     */
    @Override
    public List queryTypeNum() {
        return shoppingHotWordMapper.selectTypeNum();
    }
}
